using UnityEngine;

namespace Manager
{
    /// <summary>
    ///  输入系统单例
    /// </summary>
    public class InputManager
    {

        //原始移动输入向量
        private Vector2 originMoveVec;
        //原始移动前方向输入向量
        public float originMoveForward;
        //原始移动右方向输入向量
        public float originMoveRight;
        //用于缓冲移动
        public float moveMagnitude;
        //是否冲刺
        public bool isSprintf;
        //是否瞄准
        public bool isAiming;
        //是否投掷
        public bool isThrowable;
        //是否交互
        public bool isInteract;
        

        //原始移动视角向量
        private Vector2 originViewVec;
        //原始移动前方向输入向量
        public float originViewRight;
        //原始移动右方向输入向量
        public float originViewUp;
        
        
        //输入系统
        private static InputMap inputMap;

        private static InputManager instance;

        public static InputManager Instance
        {
            get
            {
                if (instance != null) return instance;
                instance = new InputManager();

                //启用并实例化输入系统
                inputMap = new InputMap();
                inputMap.Enable();
                return instance;
            }
        }

        //输入系统更新参数
        public void HandleUpdate()
        {
            UpdateMoveVec();
            UpdateViewVec();
            UpdateSprintfButton();

        }
        
        //更新原始移动输入
        private void UpdateMoveVec()
        {
            originMoveVec = inputMap.Free.Move.ReadValue<Vector2>();
            
            originMoveRight = originMoveVec.x;
            
            originMoveForward = originMoveVec.y;

            moveMagnitude =
                Mathf.Clamp(Mathf.Sqrt(originMoveForward * originMoveForward + originMoveRight * originMoveRight), 0,
                    1);

        }
        
        
        //更新视角输入
        private void UpdateViewVec()
        {
            originViewVec = inputMap.Free.View.ReadValue<Vector2>();

            originViewRight = originViewVec.x;

            originViewUp = originViewVec.y;
            
            
           
        }
        
        
        //更新按钮输入
        private void UpdateSprintfButton()
        {   
            //冲刺
            isSprintf = inputMap.Free.Sprintf.IsPressed();
            //瞄准
            isAiming = inputMap.Free.Aim.IsPressed();
            //投掷
            isThrowable = inputMap.Free.Throw.IsPressed();
            //交互
            isInteract = inputMap.Free.Interact.IsPressed();
        }
        
        
    }
}